package com.easytopit.work.controller;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.easytopit.work.domain.JSONResult;
import com.easytopit.work.domain.User;
import com.easytopit.work.exception.BizException;
import com.easytopit.work.service.AdminUserService;

import sun.text.normalizer.UTF16;

@Controller
public class AdminUserController {
	@Autowired
	private AdminUserService userService;
	
	@RequestMapping("/q.htm")
	public String qeury(){
		System.out.println("adminUser/Aj");
		return "adminUser/Aj";
		
	}
	@RequestMapping("/add.do")
	public void add(User user,OutputStream os)throws Exception{
		
		try{
		Integer insert = userService.insert(user);	
		os.write("1".getBytes("utf-8"));
		} catch (Exception e) {
			
		os.write("0".getBytes("utf-8"));
		e.printStackTrace();
		}
	}
	@RequestMapping("/query.do")
	public void query(String name,Integer page,Integer rows,OutputStream os){
		
		JSONResult query;
		try {
			query = userService.query(name, page,rows);
			String string = JSONObject.toJSONString(query).toString();
			os.write(string.getBytes("utf-8"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@RequestMapping("/update.do")
	public void alter(User user,OutputStream os)throws Exception{
		try {
			 userService.amend(user.getId(), user.getName(), user.getPhone(),user.getAge(),user.getPasswd());
			os.write("1".getBytes("utf-8"));
		} catch (Exception e) {
			os.write("0".getBytes("utf-8"));
		}
	}
	
	@RequestMapping("/del.do")
	public void del(Integer id,OutputStream os)  throws Exception{
		boolean remove = userService.remove(id);
		try {
			os.write("1".getBytes("UTF-8"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			os.write("0".getBytes("UTF-8"));
		}
	}
	
	
	@RequestMapping("/dels.do")
	public void dels(Integer id[],OutputStream os)  throws Exception{
		
		
		try {
			for(Integer integer :id){
				boolean remove = userService.remove(integer);	
			}
			os.write("1".getBytes("UTF-8"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			os.write("0".getBytes("UTF-8"));
		}
	}
	
	
}
